import QtQuick 1.1
import com.meego 1.0
import "CommomUtil.js" as Util
import "database.js" as DBUtil
import "Modulelib.js" as ModuleUtil
import "const.js" as ConstUtil
import "Citys.js" as CityUtil

// 车型详细页面
Page {
    id: module_info_page

    property bool isFavorited: false

    tools: ModuleInfoToolBar{
        id: toolbar_info
        isExite: module_info_page.isFavorited
        onFavorited:{
            if(module_info_page.isFavorited){
                favorites_dialog.open();
            }else{
                DBUtil.addCarFavorite(ModuleUtil.current_series_id,ModuleUtil.current_series_name,module_info_page.infoImage,
                                      module_info_page.seriesName, vehicles_price.text);
                module_info_page.isFavorited = true
            }
        }
    }

    ConfirmDialog{
        id: favorites_dialog
        diaContent: "您将把该车型从收藏夹中删除?"
        onAccepted: {
                DBUtil.removeFavorite(ModuleUtil.current_series_id,ConstUtil.FavoriteType.Car);
                module_info_page.isFavorited = false
        }
    }

    orientationLock : PageOrientation.LockPortrait

    property int fontSize: 16

    property string infoId: ""

    property string infoName: ""

    property string seriesName: ""

    property int infoPrice:  0

    property string infoImage: ""

    property int infoPhotoNum: 0

    // 经销商信息
    property int dealerId: 0

    property string dealerName: ""

    ColorAnimation { from: "white"; to: "black"; duration: 200 }

    TitleBar{
        id: module_info_title
        anchors.top: module_info_page.top; anchors.left: module_info_page.left
        width: module_info_page.width; height: 71
        titleName: ModuleUtil.current_series_name
    }

    Rectangle{
        id: detail_rect
        width: parent.width; height: 165
        anchors.top: module_info_title.bottom; anchors.left: parent.left
        z: 10
    }

    Rectangle{
        id: vehicles_image_rect
        width: 120; height: 90
        color: "#b9c6e2"
        border.width: 4; border.color: "#807d7d"
        radius: 8
        anchors.top: detail_rect.top; anchors.topMargin: 25
        anchors.left: detail_rect.left; anchors.leftMargin: 35
        z:15
        Image {
            id: vehicles_image
            source: module_info_page.infoImage
            anchors.centerIn: parent
            width: 80; height: 60
            z: 20
        }
        MouseArea{
            id: image_mouseArea
            anchors.fill: parent
            onClicked: { // 进入图片查看
                ModuleUtil.pic_type = 1;
                Util.openFile("ModulePicList.qml");
                console.log("go to cars pic page");
            }
        }

        states: [
            State {
                name: 'pressed'; when: image_mouseArea.pressed
                PropertyChanges { target: vehicles_image_rect; focus: true; opacity: 0.5}
            }
        ]
    }

    Text{
        id: photo_num
        text: module_info_page.infoPhotoNum + "张图片"
        width: vehicles_image_rect.width; height: 50
        font{pointSize: 16; bold: true}
        anchors{left: vehicles_image_rect.left; top: vehicles_image_rect.bottom; topMargin: 12}
        horizontalAlignment: Text.AlignHCenter
        z: 20
    }


    Text {
        id: vehicles_name
        text: module_info_page.infoName
        color: "#000000"
        font{pointSize: 20; bold: true}
        anchors{left: vehicles_image_rect.right; top: vehicles_image_rect.top; topMargin: 5; leftMargin: 14}
        height: 50; width: 250
        wrapMode: Text.WordWrap
        horizontalAlignment: Text.AlignLeft; verticalAlignment: Text.AlignVCenter
        z: 20
    }

    Text {
        id: vehicles_desc
        text: "厂商指导价:"
        font{pointSize: 15; bold: true}
        anchors{left: vehicles_name.left; top: vehicles_name.bottom; topMargin: 20}
        height: 20
        color: "#abb2b3"
        horizontalAlignment: Text.AlignLeft
        z: 20
    }

    Text{
        id: vehicles_price
        text: (module_info_page.infoPrice / 10000) + "万"
        font{pointSize: 22; bold: true}
        color: "red"
        anchors{left: vehicles_desc.right; bottom: vehicles_desc.bottom;}
        height: 30
        horizontalAlignment: Text.AlignLeft; verticalAlignment: Text.AlignVCenter
        z: 20
    }

    Rectangle{
        id: series_info_rect
        width: 451; height: 326
        anchors.left: parent.left; anchors.leftMargin: 14
        anchors.top: detail_rect.bottom; anchors.topMargin: 10
        BorderImage {
            id: series_info_top
            source: "../images/modelslib/info_top.png"
            width: parent.width; height: 25
            anchors.top: parent.top; anchors.left: parent.left
        }

        BorderImage {
            id: series_info_mid
            source: "../images/modelslib/info_mid.png"
            width: parent.width; height: 230
            anchors.top: series_info_top.bottom; anchors.left: parent.left
        }

        BorderImage {
            id: series_info_down
            source: "../images/modelslib/info_down.png"
            width: parent.width; height: 70
            anchors.top: series_info_mid.bottom; anchors.left: parent.left
            anchors.topMargin: 1
        }

        Rectangle{
            id:series_info_show_rect
            width: series_info_rect.width - 12; height: series_info_rect.height - 30
            anchors.right: series_info_rect.right; anchors.bottom: series_info_rect.bottom
            opacity: 0
        }

        Grid{
            id: series_info_show
            anchors.fill: series_info_show_rect
            columns: 2

            spacing: 1

            Repeater{
                model: series_info_data

                delegate: ModuleInfoItem{
                    infoId: propId
                    infoTitle: propName
                    infoContent: propContent
                }
            }


        }

        Item{
            id: config_info_rect
            width: parent.width; height: 70
            anchors.left: parent.left; anchors.bottom: parent.bottom
            Text {
                id: config_info
                text: "查看详细参数配置"
                color: "#000000"
                font{pointSize: 22; bold: true}
                anchors.centerIn: config_info_rect
            }
            MouseArea{
                id: itemMouseArea
                anchors.fill: parent
                onClicked: { // 查看详细配置信息
                    Util.openFile("ModuleConfigPage.qml");
                }
            }

            Rectangle{
                id: mouseAreaPressed
                anchors.fill: parent
                color: "#000"
                opacity: 0
            }

            states: [
                State {
                    name: 'pressed'; when: itemMouseArea.pressed
                    PropertyChanges { target: mouseAreaPressed; focus: true; opacity: 0.5}
                }
            ]
        }
    }

    ListModel{
        id: series_info_data
    }

    Rectangle{
        id: dealer_info_rect
        width: series_info_rect.width; height: 200
        anchors.left: series_info_rect.left; anchors.top: series_info_rect.bottom
        Rectangle{
            anchors{fill: parent; topMargin: 10}
            radius: 20
            gradient: Gradient {
                GradientStop { position: 0.0; color: "white" }
                GradientStop { position: 1.0; color: "#c0c5c6" }
            }
            border{color: "#c0c5c6"; width: 1}
            Column{
                spacing: 1
                Item{
                    id: dealer_title_rect
                    width: series_info_rect.width; height: 40
                    Text {
                        id: dealer_title
                        text: "本地4S店经销商"
                        color: "#abb2b3"
                        font{pointSize: 18; bold: true}
                        anchors.centerIn: dealer_title_rect
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                    }

                }
                Item{
                    id: dealer_info_rect1
                    width: series_info_rect.width; height: 80
                    Text{
                        id: dealer_text
                        text: module_info_page.dealerName
                        color: "#000000"
                        font{pointSize: 18; bold: true}
                        anchors.centerIn: dealer_info_rect1
                    }
                    MouseArea{
                        id: dealerInfoArea
                        anchors.fill: parent
                        onClicked: { // 打开经销商页面
                            ModuleUtil.current_dealer_id = module_info_page.dealerId;
                            ModuleUtil.current_dealer_name = module_info_page.dealerName;
                            Util.openFile("DealerInfoPage.qml");
                        }
                    }

                    states: [
                        State {
                            name: 'pressed'; when: dealerInfoArea.pressed
                            PropertyChanges { target: dealerInfoPressed; focus: true; opacity: 0.5}
                        }
                    ]

                    Rectangle{
                        id: dealerInfoPressed
                        anchors.fill: parent
                        color: "#000"
                        opacity: 0
                    }
                }
                Rectangle{
                    width: series_info_rect.width; height: 1
                    color: "#c0c5c6"
                }

                Item{
                    id: dealer_info_more
                    width: series_info_rect.width; height: 68
                    Text {
                        id: dealer_info_desc
                        text: "显示更多经销商"
                        color: "#000000"
                        font{pointSize: 20; bold: true}
                        anchors.centerIn: dealer_info_more
                    }
                    MouseArea{
                        id: moreDealerArea
                        anchors.fill: parent
                        onClicked: { // 查看更多经销商
                            ModuleUtil.dealerPage_type = 0;
                            Util.openFile("ModuleDealerPage.qml");
                        }
                    }


                    states: [
                        State {
                            name: 'pressed'; when: moreDealerArea.pressed
                            PropertyChanges { target: moreDealerPressed; focus: true; opacity: 0.5}
                        }
                    ]

                    Rectangle{
                        id: moreDealerPressed
                        anchors.fill: parent
                        color: "#000"
                        opacity: 0
                    }
                }

            }
        }
    }


    BusyLoading{
        id: loading
        anchors.centerIn: parent
        visible:false
    }

    Component.onCompleted: {
        loading.loading();
        module_info_page.isFavorited = DBUtil.isFavoriteExists(ModuleUtil.current_series_id,ConstUtil.FavoriteType.Car);
        module_info_page.getModuleInfoData();
        module_info_page.getSpecInfoData();
        module_info_page.getDealerInfoData();
    }

    function getModuleInfoData(){
        Util.getXmlStringByUrl(ModuleUtil.getSpecUrl(), moduleInfoCallback);
    }

    function moduleInfoCallback(responseXml){
        if(responseXml !== null){
        var a = responseXml.documentElement;
            if(a.childNodes[0] !== null && a.childNodes[0].childNodes.length > 12){
                module_info_page.infoId = a.childNodes[0].childNodes[1].childNodes[0].nodeValue;
                module_info_page.infoName = a.childNodes[0].childNodes[3].childNodes[0].nodeValue;
                if(a.childNodes[0].childNodes[5] !== null && a.childNodes[0].childNodes[5].childNodes.length > 0)
                module_info_page.seriesName = a.childNodes[0].childNodes[5].childNodes[0].nodeValue;
                if(a.childNodes[0].childNodes[7] !== null && a.childNodes[0].childNodes[7].childNodes.length > 0)
                module_info_page.infoPrice = a.childNodes[0].childNodes[7].childNodes[0].nodeValue;
                if(a.childNodes[0].childNodes[9] !== null && a.childNodes[0].childNodes[9].childNodes.length > 0)
                module_info_page.infoImage = a.childNodes[0].childNodes[9].childNodes[0].nodeValue;
                if(a.childNodes[0].childNodes[11] !== null && a.childNodes[0].childNodes[11].childNodes.length > 0)
                module_info_page.infoPhotoNum = a.childNodes[0].childNodes[11].childNodes[0].nodeValue;
            }
        }
    }

    function getSpecInfoData(){
        Util.getXmlStringByUrl(ModuleUtil.getSpecInfoUrl(), specInfoCallback);
    }

    function specInfoCallback(responseXml){
        if(responseXml !== null){
        var a = responseXml.documentElement;
            var sId = ""; var sName = ""; var sContent = "";
        for(var i = 0; i < a.childNodes[0].childNodes.length; ++i){
            if(a.childNodes[0].childNodes[i] !== null && a.childNodes[0].childNodes[i].childNodes.length > 5){
                if(a.childNodes[0].childNodes[i].childNodes[1] !== null && a.childNodes[0].childNodes[i].childNodes[1].childNodes.length > 0 )
                    sId = a.childNodes[0].childNodes[i].childNodes[1].childNodes[0].nodeValue;
                else
                    sId = "";
                if(a.childNodes[0].childNodes[i].childNodes[3] !== null && a.childNodes[0].childNodes[i].childNodes[3].childNodes.length > 0 )
            sName = a.childNodes[0].childNodes[i].childNodes[3].childNodes[0].nodeValue;
                else
                    sName = "";
                if(a.childNodes[0].childNodes[i].childNodes[5] !== null && a.childNodes[0].childNodes[i].childNodes[5].childNodes.length > 0 )
            sContent = a.childNodes[0].childNodes[i].childNodes[5].childNodes[0].nodeValue;
                else
                    sContent = "";
                series_info_data.append({"propId":sId,"propName":sName+":","propContent":sContent});
                if(sId === "1" || sId === "4"){
                series_info_data.append({"propId":0,"propName":" ","propContent":""});
            }
            }
        }
        }
        loading.finish();

    }

    function getDealerInfoData(){
        Util.getXmlStringByUrl(ModuleUtil.getDealerInfoUrl(CityUtil.getCurrentCityId(), 1), dealerInfoCallback);
    }

    function dealerInfoCallback(responseXml){
        var a = responseXml.documentElement;
        if(a.childNodes[0]!== null && a.childNodes[0].childNodes[0] !== null && a.childNodes[0].childNodes[0].childNodes.length > 0)
        {module_info_page.dealerId = a.childNodes[0].childNodes[0].childNodes[0].childNodes[0].nodeValue;
        module_info_page.dealerName = a.childNodes[0].childNodes[0].childNodes[1].childNodes[0].nodeValue;
        }
    }
}
